package com.pachong.android.frameworkbase.customviews.iconcontent;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.pachong.android.frameworkbase.R;

/**
 * TODO
 *
 * @Author: PV
 * @E-mail: pengveivei@sina.cn
 * @CreateDate: 2016-06-28
 * @Version: 1.0.0
 */
public class IconContentView extends LinearLayout {

    //存放icon的容器
    private RelativeLayout mLytMain = null;
    private RelativeLayout mLytIconContainer = null;
    private LinearLayout mLytMainContent = null;
    private LinearLayout mLytSubContent = null;
    private LinearLayout mLytSubSubContent = null;



    private ImageView mIvIcon = null;


    public IconContentView(Context context) {
        this(context, null);
        setupViews(null);
    }


    public IconContentView(Context context, AttributeSet attrs) {
        super(context, attrs);
        View.inflate(getContext(), R.layout.icon_content_view, this);
        setupViews(attrs);
    }


    private void setupViews(AttributeSet attrs) {
        mIvIcon = (ImageView) findViewById(R.id.ivIcon);
        if (attrs != null) {
            TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.IconTextArrowButton);

            int iconResId = a.getResourceId(R.styleable.IconTextArrowButton_android_icon, -1);
            if (iconResId != -1) {
                setIcon(iconResId);
                int iconSize = a.getDimensionPixelSize(R.styleable.IconTextArrowButton_iconSize, -1);
                if (iconSize > 0) {
                    RelativeLayout.LayoutParams iconParams = (RelativeLayout.LayoutParams) mIvIcon.getLayoutParams();
                    iconParams.width = iconSize;
                    iconParams.height = iconSize;
                    getIconContainer().updateViewLayout(mIvIcon, iconParams);
                    mIvIcon.setVisibility(VISIBLE);
                }
            } else {
                if (mIvIcon != null) {
                    mIvIcon.setVisibility(GONE);
                }
            }
            a.recycle();
        }

    }


    public void setIcon(int resId) {
        mIvIcon.setImageResource(resId);
        mIvIcon.setVisibility(VISIBLE);
//        invalidate();
    }

    public LayoutParams getDefParams() {
        return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    }


    public RelativeLayout getIconContainer() {
        return (RelativeLayout) findViewById(R.id.lytIconContainer);
    }

    public LinearLayout getMainContentLayout() {
        return (LinearLayout) findViewById(R.id.lytMainContent);
    }

    public LinearLayout getSubContentLayout() {
        return (LinearLayout) findViewById(R.id.lytSubContent);
    }

    public LinearLayout getSubSubContentLayout() {
        return (LinearLayout) findViewById(R.id.lytSubSubContent);
    }

    protected LinearLayout getMainLayout() {
        return (LinearLayout) findViewById(R.id.lytMain);
    }


    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
